#include "NumericKeyboard.h"
#include <QPushButton>
#include <QGridLayout>
#include <QApplication>
#include <QStyle>

NumericKeyboard::NumericKeyboard(QWidget *parent)
    : QWidget(parent), targetLineEdit(nullptr)
{
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
    setStyleSheet(R"(
        NumericKeyboard {
            background-color: #f0f0f0;
            border: 2px solid #cccccc;
            border-radius: 5px;
        }
        QPushButton {
            font-size: 18px;
            font-weight: bold;
            border: 1px solid #cccccc;
            border-radius: 3px;
            background-color: white;
            min-width: 40px;
            min-height: 40px;
        }
        QPushButton:hover {
            background-color: #e0e0e0;
        }
        QPushButton:pressed {
            background-color: #d0d0d0;
        }
        QPushButton#backspace {
            background-color: #ff6b6b;
            color: white;
        }
        QPushButton#clear {
            background-color: #4ecdc4;
            color: white;
        }
        QPushButton#close {
            background-color: #45b7d1;
            color: white;
        }
    )");

    QGridLayout *mainLayout = new QGridLayout(this);
    mainLayout->setSpacing(4);
    mainLayout->setContentsMargins(6, 6, 6, 6);

    // 创建数字按钮 1-9
    for (int i = 1; i <= 9; ++i) {
        digitButtons[i] = createButton(QString::number(i), SLOT(onDigitButtonClicked()));
        mainLayout->addWidget(digitButtons[i], (i-1)/3, (i-1)%3);
    }

    // 第4行按钮
    // 0 按钮
    digitButtons[0] = createButton("0", SLOT(onDigitButtonClicked()));
    mainLayout->addWidget(digitButtons[0], 3, 0);

    // 退格按钮
    backspaceButton = createButton("⌫", SLOT(onBackspaceClicked()));
    backspaceButton->setObjectName("backspace");
    mainLayout->addWidget(backspaceButton, 3, 1);

    // 清空按钮
    clearButton = createButton("C", SLOT(onClearClicked()));
    clearButton->setObjectName("clear");
    mainLayout->addWidget(clearButton, 3, 2);

    setLayout(mainLayout);
    setFixedSize(160, 200);
    hide();
}

QPushButton *NumericKeyboard::createButton(const QString &text, const char *member)
{
    QPushButton *button = new QPushButton(text, this);
    button->setFixedSize(45, 45);
    connect(button, SIGNAL(clicked()), this, member);
    return button;
}

void NumericKeyboard::setTargetLineEdit(QLineEdit *lineEdit)
{
    targetLineEdit = lineEdit;
}

void NumericKeyboard::showAtPosition(const QPoint &pos)
{
    move(pos);
    show();
    raise();
    activateWindow();
}

void NumericKeyboard::onDigitButtonClicked()
{
    if (!targetLineEdit) return;

    QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
    if (clickedButton) {
        QString digit = clickedButton->text();
        targetLineEdit->insert(digit);
        emit keyPressed(digit);
    }
}

void NumericKeyboard::onBackspaceClicked()
{
    if (targetLineEdit) {
        targetLineEdit->backspace();
    }
}

void NumericKeyboard::onClearClicked()
{
    if (targetLineEdit) {
        targetLineEdit->clear();
    }
}
